
#ifndef UPDATE_MAINWINDOW_H
#define UPDATE_MAINWINDOW_H

#include <QWidget>
#include <QTime>
#include <QTimer>
#include <QJsonArray>
#include "ui_mainwindow.h"
#include "download_manager.h"

class GoodsWidget;
class ImageProvider;

class MainWindow : public QWidget
{
Q_OBJECT
public:
    MainWindow(QWidget *parent = 0);
    ~MainWindow();

private:
    void initUi();
    // 转换单位;
    QString transformUnit(qint64 bytes , bool isSpeed = false);
    // 转换时间;
    QString transformTime(qint64 seconds);
    // 判断需要更新
    bool checkNeedUpdate(const QString &version);
    // 获取本地版本
    void getLocalVersion();

    void settings();

    void downLoadPage(const bool & isDownload);

    void showDefaultTextNew();
    void showDefaultPicNew1();
    void showDefaultPicNew2();

protected:
    // 下载
    void download(const QString &name);

    void mouseMoveEvent(QMouseEvent* event) override;

    void mouseReleaseEvent(QMouseEvent*) override;

    void mousePressEvent(QMouseEvent* event) override;

private slots:
    void onStopDownload();
    void onStartDownload();
    void onCloseDownload();
    void onCancelDownload();

    void onDownloadProcess(qint64 bytesReceived, qint64 bytesTotal);
    void onReplyFinished(int);

    void itemClicked(QListWidgetItem *item);
    // 更新版本信息查询
    void updateVersionCheck(const QJsonArray &array);
    void showGoods(const QJsonObject &obj);
    void showTextNews(const QJsonObject &obj);
    void showPicNews(const QJsonObject &obj);
    // 解压完成
    void unzipFinished(const bool& isok);

    void on_btn_enter_clicked();
    void on_btn_home_clicked();
    void on_btn_shops_clicked();
    void on_btn_mini_clicked();
    void on_btn_close_clicked();

private:
    Ui::mainwindow ui;

    DownloadManager* m_downloadManager{nullptr};
    uint m_timeInterval;
    qint64 m_currentDownload;
    qint64 m_intervalDownload;
    QTime m_timeRecord;
    QString m_version;
    QMap<QString,QPair<QString,QString>> m_versionMap{};

    QStringList m_list;
    bool m_first{true};
    bool m_big_version_{false};
    bool m_need_update_{false};
    bool first{true};

    QTimer timer;
    QVector<GoodsWidget *> good_widgets_{};
    ImageProvider *image_provider_{nullptr};

    QPoint last_position_;           //窗口上一次的位置
    bool left_button_pressed_{false}; //鼠标左键按下
};

#endif //UPDATE_MAINWINDOW_H
